/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FindUser.java
 *
 * Created on 16/06/2011, 03:39:04 AM
 */

package GUI.ATTENDANT;

import Procedures.DBConection;
import entities.User;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author SIX-14
 */
public class FinderUser extends javax.swing.JPanel {

    /** Creates new form FindUser */
    public FinderUser() {
        initComponents();
    }
    public FinderUser(DBConection myConection, int action, JFrame paternFrame) {
        initComponents();
        setMyConection(myConection);
        setPaternFrame(paternFrame);
        setAction(action);
        }

    private DBConection myConection;
    private String tablename="";
    private int action;
    private JFrame paternFrame;


    public JFrame getPaternFrame() {
        return paternFrame;
    }

    public void setPaternFrame(JFrame paternFrame) {
        this.paternFrame = paternFrame;
    }

    public int getAction() {
        return action;
    }

    public void setAction(int action) {
        this.action = action;
    }

    public DBConection getMyConection() {
        return myConection;
    }

    public void setMyConection(DBConection myConection) {
        this.myConection = myConection;
    }

    public String getTablename() {
        return tablename;
    }

    public void setTablename(String tablename) {
        this.tablename = tablename;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jComboBox2 = new javax.swing.JComboBox();

        jLabel1.setText("Identificador del");

        jButton1.setText("Buscar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1MousePressed(evt);
            }
        });

        jLabel2.setText("Mostar registros de todos los");

        jLabel3.setText("Buscando por:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Identificador", "Nombre", "Apellido paterno", "Apellido materno", "Especialidad", "Activo" }));

        jLabel4.setText("Escriba el valor de busqueda (deje en blanco para mostrar todos)");

        jButton2.setText("Buscar");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Alumno", "Encargado", "Invitado", "Maestro", " " }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel4)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(jButton1)))
                .addContainerGap(68, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addContainerGap(29, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed
        if(jComboBox2.getSelectedItem().toString().equals("Alumno")){
            setTablename("student");
        }else if(jComboBox2.getSelectedItem().toString().equals("Maestro")){
            setTablename("teacher");
        }else if(jComboBox2.getSelectedItem().toString().equals("Invitado")){
            setTablename("inviter");
        }else if(jComboBox2.getSelectedItem().toString().equals("Encargado")){
            setTablename("teacher");
        }
        if(!jTextField1.getText().isEmpty()){
            User user =new User();

                    getMyConection().sendSQLQuery("SELECT * FROM "+getTablename()+ " where id= "+ jTextField1.getText() +";");
                    if(getMyConection().getLastResultSetColumnStrings(1)!=null){
                    user.setIdentifier(Integer.parseInt(getMyConection().getLastResultSetColumnStrings(1)[0]));
                    user.setTypeUser(getMyConection().getLastResultSetColumnStrings(2)[0]);
                    user.setName(getMyConection().getLastResultSetColumnStrings(3)[0]);
                    user.setPaternLastname(getMyConection().getLastResultSetColumnStrings(4)[0]);
                    user.setMaternLastname(getMyConection().getLastResultSetColumnStrings(5)[0]);
                    user.setEspeciality(getMyConection().getLastResultSetColumnStrings(6)[0]);
                    user.setActive(Boolean.parseBoolean(getMyConection().getLastResultSetColumnStrings(7)[0]));
                    String[] atendant = new String[2];
                    if(jComboBox2.getSelectedItem().toString().equals("Encargado")){
                        getMyConection().sendSQLQuery("SELECT USERNAME, PASSWORD FROM ATENDANT where id= "+ jTextField1.getText() +";");
                        atendant[0]=getMyConection().getLastResultSetColumnStrings(1)[0];
                        atendant[1]=getMyConection().getLastResultSetColumnStrings(2)[0];
                    }else{
                        atendant[0]="";
                        atendant[1]="";
                    }
            try{
                getPaternFrame().getContentPane().removeAll();
            }catch(Exception e){
            }
            UserGui usergui =new UserGui(getMyConection(), getAction(),getPaternFrame(),user, atendant);
            getPaternFrame().add(usergui);
            getPaternFrame().pack();

        }else{
            JOptionPane.showMessageDialog(null, "no se encuentro ningun "+jComboBox1.getSelectedItem().toString()+" con matricula "+jTextField1.getText(), "Verifiue tipo de usuario e identificador", 1);
        }
        }
        
    }//GEN-LAST:event_jButton1MousePressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
